package ii.oo.liu;

import java.io.*;

/**
 * Created by Admin on 1/6 0006.
 */
public class Test {

    private static int position = -1;

    public static void main(String[] args) {
        File sourceFile = new File("D:\\test.txt");
        File targetFile = new File("E:\\test.txt");
        FileInputStream fis = null;
        FileOutputStream fos = null;
        byte[] buf = new byte[1];
        try {
            fis = new FileInputStream(sourceFile);
            fos = new FileOutputStream(targetFile);
            while (fis.read(buf) != -1) {
                fos.write(buf);
                if (targetFile.length() == 3) {
                    position = 3;
                    throw new FileAccessException();
                }
            }
        } catch (FileAccessException e) {
            keepGoing(sourceFile, targetFile, position);
        } catch (IOException e) {
        } finally {
            try {
                if (fis != null)
                    fis.close();
                if (fos != null)
                    fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private static void keepGoing(File source, File target, int position) {
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        try {
            RandomAccessFile readFile = new RandomAccessFile(source, "rw");
            RandomAccessFile writeFile = new RandomAccessFile(target, "rw");
            readFile.seek(position);
            writeFile.seek(position);
            byte[] buf = new byte[1];
            while (readFile.read(buf) != -1) {
                writeFile.write(buf);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

class FileAccessException extends Exception {

}
